package com.yidu.commom;
 
import java.io.IOException;





import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 

public class Tools { 
	static Logger logger = Logger.getLogger(Tools.class);
	//static Logger logger= Logger.getLogger(Tools.class);
	
	/**
	 * spring连接
	 * @param xmlName
	 * @return
	 */
	public static ApplicationContext getAction(String xmlName){
		ApplicationContext ac = new ClassPathXmlApplicationContext(xmlName);
		return ac;
		
	}
	 
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str){

		if(str==null) return true;

		if("".equals(str)) return true;

		return false;

	}
	
	
	/*public static void close(Connect con) throws SQLException, IOException{
		if(con.getCon()!=null){
			con.getCon().close();
		}
		if(con.getPstm()!=null){
			con.getPstm().close();
		}
		if(con.getRs()!=null){
			con.getRs().close();
		}
		if(con.getIs()!=null){
			con.getIs().close();
		}
		
	}*/
	/**
	 * gson 封装
	 * @param response
	 * @param obj
	 * @throws IOException
	 */
	/*public static void write(HttpServletResponse response,Object obj) throws IOException {
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		String json = gson.toJson(obj);
		out.write(json);
		logger.info(json);
		out.close();
	}*/
	
	/**
	 * date 
	 * @param date
	 * @return String
	 */
	public static String isDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String dateStr = sdf.format(date);


		return dateStr;
	}
	/**   
	 * jdbc连接 sqlserver数据库步�?
	 * 1.加载数据库驱动程�?
	 * 2.得到数据库连�?
	 * 3.得到数据库操作对�?
	 * 4.设置参数
	 * 5.执行SQL语句
	 */ 
//	static String className = "";
//	static String url = "";
//	static String userName = "";
//	static String password = "";
//	
//	static{
//		Properties pro = new Properties();
//		InputStream is = Tools.class.getClassLoader().getResourceAsStream("config/oracle.properties");
//		
//		try {
//			pro.load(is);
//			//得到节点 目录
//			className = pro.getProperty("className");
//			url = pro.getProperty("url");
//			userName = pro.getProperty("userName");
//			password = pro.getProperty("password");
//			
//			is.close();
//
//		} catch (IOException e) { 
//			e.printStackTrace();
//		}
//		
//	
//	}
	
	/**
	 *  加载驱动
	 * @return 
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static Connection getConnection( ) throws ClassNotFoundException, SQLException{
		Connection con = null;
		Class.forName("oracle.jdbc.driver.OracleDriver");
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		String user = "c##scott";
		String password = "tiger";
		con = DriverManager.getConnection(url, user, password);
		
		return con ;

	}
	
	 
	
	/**
	 * 关闭连接
	 * @param con
	 * @param cstm
	 * @throws SQLException
	 */
	public static void close(Connection con,CallableStatement cstm) throws SQLException{
		if(con!=null) con.close();
		
		if(cstm!=null) cstm.close();
	}
	public static void close(Connection con,CallableStatement cstm,ResultSet rs) throws SQLException{
		if(con!=null) con.close();
		
		if(cstm!=null) cstm.close();
		
		if(rs!=null)rs.close();
	}
}
